﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Metal_Archives
{
    public partial class Search : PhoneApplicationPage
    {
        public Search()
        {
            InitializeComponent();
        }

        private void txtSearch_GotFocus(object sender, RoutedEventArgs e)
        {
            TextBox sndr = sender as TextBox;
            if (sndr.Text == "search")
            {
                sndr.Text = "";
            }
        }

        private void txtSearch_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox sndr = sender as TextBox;
            if (sndr.Text == "")
            {
                sndr.Text = "search";
            }
        }

        private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            PivotItem from = e.RemovedItems[0] as PivotItem;
            PivotItem to = e.AddedItems[0] as PivotItem;

            if (from != null)
            {
                this.Focus();

                String preVal = "";

                //Retrieve value in search control of previous PivotItem
                switch (from.Header.ToString())
                {
                    case "band":
                        preVal = this.txtBandSearch.Text;
                        break;
                    case "genre":
                        preVal = this.txtGenreSearch.Text;
                        break;
                    case "lyrical themes":
                        preVal = this.txtLyricalSearch.Text;
                        break;
                    case "album":
                        preVal = this.txtAlbumSearch.Text;
                        break;
                    case "song":
                        preVal = this.txtSongSearch.Text;
                        break;
                    case "label":
                        preVal = this.txtLabelSearch.Text;
                        break;
                    case "artist":
                        preVal = this.txtArtistSearch.Text;
                        break;
                }

                if (preVal == "") { preVal = "search"; }

                //Set value in search control of new PivotItem
                switch (to.Header.ToString())
                {
                    case "band":
                        this.txtBandSearch.Text = preVal;
                        break;
                    case "genre":
                        this.txtGenreSearch.Text = preVal;
                        break;
                    case "lyrical themes":
                        this.txtLyricalSearch.Text = preVal;
                        break;
                    case "album":
                        this.txtAlbumSearch.Text = preVal;
                        break;
                    case "song":
                        this.txtSongSearch.Text = preVal;
                        break;
                    case "label":
                        this.txtLabelSearch.Text = preVal;
                        break;
                    case "artist":
                        this.txtArtistSearch.Text = preVal;
                        break;
                }
            }
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                TextBox sndr = sender as TextBox;
                String argVal = "";
                String argType = "";

                switch (sndr.Name)
                {
                    case "txtBandSearch":
                        argVal = this.txtBandSearch.Text;
                        argType = "band";
                        break;
                    case "txtGenreSearch":
                        argVal = this.txtGenreSearch.Text;
                        argType = "genre";
                        break;
                    case "txtLyricalSearch":
                        argVal = this.txtLyricalSearch.Text;
                        argType = "lyrics";
                        break;
                    case "txtAlbumSearch":
                        argVal = this.txtAlbumSearch.Text;
                        argType = "album";
                        break;
                    case "txtSongSearch":
                        argVal = this.txtSongSearch.Text;
                        argType = "song";
                        break;
                    case "txtLabelSearch":
                        argVal = this.txtLabelSearch.Text;
                        argType = "label";
                        break;
                    case "txtArtistSearch":
                        argVal = this.txtArtistSearch.Text;
                        argType = "artist";
                        break;
                }

                if (argVal.Length > 0)
                {
                    NavigationService.Navigate(new Uri("/SearchResults.xaml?argVal=" + argVal + "&argType=" + argType, UriKind.Relative));
                }
            }
        }
    }
}